import {Injectable} from '@angular/core';
import {AppConfig} from '../../config';
import {InterceptorService} from '../interceptor/interceptor-service';
import {DeviceAlarm} from '../../entity/alarm/alarm.model';

@Injectable()
export class AlarmService {
  private param = 'historys';
  private readonly headers = new Headers({
    'Content-Type': 'application/json'
  });

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) {
  }

  /**
   * 获得风机状态
   *
   * @returns {Promise<DeviceAlarm[]>}
   * @memberof TurbStatusService
   */
  getDeviceAAlarm(deviceId: number[], krCode: string, edDesc: string, faultLevelId: number, startTime: number, endTime: number): Promise<DeviceAlarm[]> {
    return this.http.post(`${this.url}/deviceAAlarm`, JSON.stringify({
      deviceId: deviceId,
      krCode: krCode,
      faultLevelId: faultLevelId,
      edDesc: edDesc,
      startTime: startTime,
      endTime: endTime
    }), {headers: this.headers}).toPromise()
      .then(response => {
        return response.code === '200' ? response.data as DeviceAlarm[] : [];
      }).catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }
}
